#ifndef SDBULLET_H
#define SDBULLET_H

#include <vector>
#include <math.h>
#include "../src/chipmunk.h"
#include "sdObject.h"

//A timed object that dies after a set time to live
class sdTimed : public sdObject {
private:
    cpFloat ttl_; //Ticks to live
	cpFloat maxTtl_; //Maximum ticks to live
    bool dying_; //Ticking down at the moment?
    
public:
	//Contructor
    inline sdTimed(cpFloat ttl, cpBody* body) : sdObject(body), ttl_(ttl), maxTtl_(ttl), dying_(false) {}
   
	//Tick down time to live
    inline void update(cpFloat ticks) {
        if(ttl_ > 0 && dying_) {
            ttl_ -= ticks;
        }
    }
    
	inline cpFloat getTtl() {return ttl_;}
    inline void setTtl(cpFloat ttl) {
        if(!dying_) {
            ttl_ = ttl;
			maxTtl_ = ttl;
        }
    }
    
	//Gets the chunks of this object for when it is killed
    void getChunks(std::set<sdTimed*>* chunks) {
        int count = 50;
        cpFloat div = 2 * 3.14159 / count;
        cpFloat iDiv;
    
        for(int i = 0; i < count; i++) {
            cpBody* body = new cpBody();
            iDiv = i * div;
            sdTimed* bullet = new sdTimed(0.1, body);
            bullet->start();
            bullet->setMass(0.01);
        	bullet->setPos(cpv(body_->p.x + 2 * sin(iDiv), body_->p.y + 2 * cos(iDiv)));
        	bullet->addCircleShape(0, 1, cpvzero, 5);

        	bullet->setVel(cpv(1000 * sin(iDiv), 1000 * cos(iDiv)));
     	
        	chunks->insert(bullet);
        }
    }
	
	//Start ticking down
	inline void start() {dying_ = true;}

	inline bool dying() {return dying_;}
	inline cpFloat maxTtl() {return maxTtl_;}
    
    inline bool dead() {return ttl_ < 0.0;}
};

#endif
